package com.hugh.edu.controller;

import com.hugh.edu.controller.param.ResumeEntity;
import com.hugh.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */
    @Autowired
    private ResumeService resumeService;

    @GetMapping("/queryResumeList")
//    @ResponseBody
    public String queryAll(HttpServletRequest request) throws Exception {
        List<ResumeEntity> resumeList = resumeService.queryResumeList();
        request.setAttribute("resumeList", resumeList);
        return "list";
    }

    @RequestMapping(value = "/saveResume" , method= RequestMethod.POST, consumes="application/x-www-form-urlencoded")
    public String  addResume( ResumeEntity resume) throws Exception {
        if(Objects.nonNull(resume)){
            if(Objects.isNull(resume.getId())){
                resumeService.addResume(resume);
            }else{
                resumeService.modifyResume(resume);
            }
        }
        return "redirect:/resume/queryResumeList?username=admin";
    }


    @RequestMapping("/deleteResume")
    public String  deleteResume( String id) throws Exception {
        ResumeEntity resume = new ResumeEntity();
        resume.setId(Long.valueOf(id));
        resumeService.deleteResume(resume);
        return "redirect:/resume/queryResumeList?username=admin";
    }

    @RequestMapping("/getResume")
    public String  getResume( String id, HttpServletRequest request) throws Exception {
        ResumeEntity resume = new ResumeEntity();
        if(!StringUtils.isEmpty(id) && Long.parseLong(id) > 0L) {
            resume = resumeService.getResumeById(Long.valueOf(id));
        }
        request.setAttribute("resume", resume);
        return "input";
    }
}
